

export const BVID_SUBTITLE_MAP_DOUBAO_CHAT_ID = 'bvid_subtitle_map_doubao_chat_id'
export const BVID_COMMENTS_MAP_DOUBAO_CHAT_ID = 'bvid_comments_map_doubao_chat_id'

import _ from 'lodash'

const createChat = async (content: string, prompt: string) => {

    const cookieKeyValue: any = {
        "d_ticket": undefined,
        "store-region": undefined,
        "store-region-src": undefined,
        "passport_csrf_token": undefined,
        "passport_csrf_token_default": undefined,
        "passport_auth_status": undefined,
        "passport_auth_status_ss": undefined,
        "sid_guard": undefined,
        "sid_tt": undefined,
        "uid_tt": undefined,
        "uid_tt_ss": undefined,
        "sessionid": undefined,
        "sessionid_ss": undefined,
        "is_staff_user": undefined,
        "sid_ucp_v1": undefined,
        "ssid_ucp_v1": undefined,
        "tt_scid": undefined,
        "i18next": undefined,
        "flow_ssr_sidebar_expand": undefined,
        "passport_fe_beating_status": undefined,
        "session_tlb_tag": undefined,
        "flow_user_country": undefined,
        "_ga_G8EP5CG8VZ": undefined,
        "gd_random": undefined,
        "ttwid": undefined,
        "hook_slardar_session_id": undefined,
        "msToken": undefined,
    }


    const cookies: any = await chrome.cookies.getAll({ url: "https://www.doubao.com/" });

    _.keys(cookieKeyValue).forEach((name) => {
        const value = _.find(cookies, { name })?.value
        if (_.isNil(value)) {
            delete cookieKeyValue[name]
        } else {
            cookieKeyValue[name] = value
        }

    })


    let cookie = _.keys(cookieKeyValue).map((key) => `${key}=${cookieKeyValue[key]}`).join('; ')

    return await fetch("https://cili-server.vercel.app/proxy", {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            "headers": {
                "accept": "*/*",
                "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-US;q=0.6",
                "agw-js-conv": "str, str",
                "cache-control": "no-cache",
                "content-type": "application/json",
                "last-event-id": "undefined",
                "pragma": "no-cache",
                "priority": "u=1, i",
                "referer": "https://www.doubao.com/chat/local_1641431668365467?type=2",
                "sec-ch-ua": "\"Not;A=Brand\";v=\"99\", \"Google Chrome\";v=\"139\", \"Chromium\";v=\"139\"",
                "sec-ch-ua-mobile": "?0",
                "sec-ch-ua-platform": "\"macOS\"",
                "sec-fetch-dest": "empty",
                "sec-fetch-mode": "cors",
                "sec-fetch-site": "same-origin",
                "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
                "cookie": cookie,
            },
            "data": {
                "messages": [
                    {
                        // "content": `{\"text\":\"详细总结以下文章内容：${content}\"}`,
                        "content": JSON.stringify({
                            text: `${prompt}\n${content}`
                        }),
                        "content_type": 2001,
                        "attachments": [],
                        "references": []
                    }
                ],
                "completion_option": {
                    "is_regen": false,
                    "with_suggest": true,
                    "need_create_conversation": true,
                    "launch_stage": 1,
                    "is_replace": false,
                    "is_delete": false,
                    "message_from": 0,
                    "use_deep_think": false,
                    "use_auto_cot": false,
                    "resend_for_regen": false,
                    "event_id": "0"
                },
                "evaluate_option": {
                    "web_ab_params": ""
                },
                "conversation_id": "0"
                // "local_conversation_id": "local_1641431668365467",
                // "local_message_id": "9aa38880-8c7b-11f0-8c33-7789a44841fb"
            },
            "method": "POST",
            "targetUrl": `https://www.doubao.com/samantha/chat/completion?aid=497858&device_id=7377217223036782134`
        }),
        redirect: "follow"
    });
}

export default {
    createChat
}

